package com.hao.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hao.commonutils.R;
import com.hao.eduservice.entity.EduCourse;
import com.hao.eduservice.entity.EduTeacher;
import com.hao.eduservice.service.EduCourseService;
import com.hao.eduservice.service.EduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author chenwenhao
 * @version 1.0.0
 * @ClassName TeacherFrontContorller.java
 * @createTime 2021/1/4,20:53
 * @Description·    讲师前台页面
 */
@RestController
@CrossOrigin
@RequestMapping("/eduservice/teacherfront")
public class TeacherFrontController {

    @Autowired
    private EduTeacherService teacherService;

    @Autowired
    private EduCourseService courseService;

    /**
     * 老师前台列表页面
     *
     * @param page  页面
     * @param limit 限制
     * @return {@link R}
     */
    @PostMapping("getTeacherFrontList/{page}/{limit}")
    public R getTeacherFrontList(@PathVariable long page,@PathVariable long limit){
        Page<EduTeacher> teacherPage = new Page<>(page,limit);
        Map<String,Object> map = teacherService.getTeacherFrontList(teacherPage);
        //返回所有数据
        return R.ok().data(map);
    }


    /**
     * 根据讲师id，查询讲师信息，以及讲的课程
     *
     * @param teacherId 老师id
     * @return {@link R}
     */
    @GetMapping("getTeacherFrontInfo/{teacherId}")
    @Cacheable(key = "'getTeacherFrontInfo'+#teacherId",value = "teacherFrontInfo")
    public R getTeacherFrontInfo(@PathVariable long teacherId){
        //讲师
        EduTeacher teacher = teacherService.getById(teacherId);
        //课程
        List<EduCourse> courseList = courseService.getCourseInfoByTeacherId(teacherId);

        return R.ok().data("teacher",teacher).data("courseList",courseList);
    }
}
